﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DepartmentSystem
{
    public partial class DepartmentInfo1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSoure();
            }
            else
            {
                GridviewControl.ResetGridView(this.GridView1);
            }
        }

        public void BindSoure()
        {
            //this.GridView1.DataSource = SQLHelper.executeReader("select deptNum, deptName, headNum, startTime from tb_department");
            // this.GridView1.DataBind();
            GridviewControl.GridViewDataBind(this.GridView1,
                SQLHelper.GetTable("select deptNum, deptName, headNum, startTime from tb_department"));
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //获得编辑列
            GridView1.EditIndex = e.NewEditIndex;
            //重新绑定数据
            BindSoure();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //退出编辑状态
            GridView1.EditIndex = -1;
            //重新绑定数据
            BindSoure();
        }
        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int deptNum = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            string sql = "delete from tb_department where deptNum='" + deptNum + "'";


            if (SQLHelper.executeNoQuery(sql) != 1)
            {
                Response.Write("<script>alert('删除失败！')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除成功！')</script>");
            }
            //重新绑定数据
            BindSoure();
        }

        protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
        {
            int deptNum = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            string deptName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
            string headNum = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();

            string startTime = "";

            if (deptName.Length == 0)
            {
                Response.Write("<script>alert('部门号不能为空！')</script>");
                return;
            }
            if (headNum.Length != 0)
            {
                DateTime dt = DateTime.Now;

                startTime = dt.ToString();
            }

            string text = "update tb_department  set deptName = '" + deptName + "', headNum = '" + headNum +
                "', startTime = '" + startTime + "' where deptNum = '" + deptNum + "'";

            if (SQLHelper.executeNoQuery(text) != 1)
            {
                Response.Write("<script>alert('更新失败！')</script>"); return;
            }
            else
            {
                Response.Write("<script>alert('更新成功！')</script>");
            }
            //退出编辑状态
            GridView1.EditIndex = -1;
            //重新绑定数据
            BindSoure();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            Server.Transfer("DepartmentAdd.aspx");
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Server.Transfer("DepartmentInfo.aspx");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Server.Transfer("WorkerInfo.aspx");
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            string deptNum = TextBox1.Text;
            string deptName = TextBox2.Text;

            string sql = "select * from tb_department where 1 = 1";

            if (deptNum.Length != 0)
            {
                sql += " and deptNum = '" + deptNum + "'";
            }

            if (deptName.Length != 0)
            {
                sql += " and deptName like '%" + deptName + "%'";
            }


            //  this.GridView1.DataSource = SQLHelper.executeReader(sql);
            //   this.GridView1.DataBind();
            GridviewControl.GridViewDataBind(this.GridView1,
                  SQLHelper.GetTable(sql));
        }
    }
}